<%@ page import="com.cc.entity.Oss" %>
<%@ page import="java.util.List" %><%--
  Created by IntelliJ IDEA.
  User: c
  Date: 2024/6/6
  Time: 9:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>存储空间列表</title>
    <script src="../js/jquery.js"></script>
    <style>
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            border: 1px solid black;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: bisque;
        }
    </style>
</head>
<body>
<h1>存储空间列表</h1>
<a href="add.jsp">创建存储空间</a>
<table>
    <thead>
    <tr>
        <th>存储空间名称</th>
        <th>存储类型</th>
        <th>容量</th>
        <th>流量</th>
        <th>当月访问次数</th>
        <th>访问权限管理</th>
    </tr>
    </thead>

        <tbody>



        </tbody>
</table>
</body>
</html>
<script>
    //页面加载
    //1.获取全部
    let ossUrl = '/oss/oss.action'
    let data = {opr: 'list'}
    $.ajax({
        "url": ossUrl,
        'type': 'get',
        'data': data,
        'dataType': 'json',
        'success': ossList,
        'error': function () {
            alert("查询失败！请联系管理员")
        }
    })
    //回调成功，加载数据
    function ossList(data) {
        console.log(data);
        $('tbody').empty();
        if(data!=null){
            data.forEach(dd => {

                let priSy = '';
                let rGgd = '';
                let rwGgdx = '';
                if (dd.permission == 'Pri'){
                    priSy = 'sy'
                }
                if (dd.permission == 'Ggd'){
                    rGgd = 'r'
                }
                if (dd.permission == 'Ggdx'){
                    rwGgdx = 'rw'
                }
                let tr = $(`<tr>
                    <td>`+dd.bucketName+`</td>
                    <td>`+dd.type+`</td>
                    <td>`+dd.capacity+`</td>
                    <td>`+dd.flow+`</td>
                    <td>`+dd.visits+`</td>
                    <td>
                        <input type="radio" name="permission-`+dd.id+`" value="Pri" `+priSy+`  onclick="changeval(`+dd.id+`,'Pri')">Pri
                        <input type="radio" name="permission-`+dd.id+`" value="R"  `+rGgd+` onclick="changeval(`+dd.id+`,'R')">R
                        <input type="radio" name="permission-`+dd.id+`" value="RW"  `+rwGgdx+` onclick="changeval(`+dd.id+`,'RW')">RW
                </td>
                    </tr>`);
                $('tbody').append(tr);
            })
        }
        //点击创建按钮  跳转到新增页面
        $('#add').click(function () {
            window.location.href = "add.jsp";
        })

        function changeval(id,permission) {
            let opr = "update";
            let params = {opr,id,permission}
            $.ajax({
                url: '/oss/oss.action',
                type: 'post',
                data: params,
                dataType: 'json',
                success:function (data) {
                    console.log(data);
                    if (data==true){
                        alert('更新成功！')
                        location.reload();
                    }else {
                        alert('更新失败！')
                        location.reload();
                    }
                },
                error:function () {
                    alert('修改失败！')
                }
            })
        }
    }
</script>